﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace FrontDisplay
{
    public static class Navigation
    {
        private static DispatcherTimer _timer;
        private static IAnimation _animation;

        private static Dictionary<UIElement, UIElement> _history = new Dictionary<UIElement, UIElement>();

        public static void Navigate(Control control)
        {
            Navigate(control, Animation.Fadeout);
        }

        public static void Navigate(Control control, IAnimation animation)
        {
            Navigate(control, animation, true);
        }

        private static void Navigate(Control control, IAnimation animation, bool useHistory)
        {
            if (Global.MainPanel.Children.Contains(control)) return;

            ThemeManager.UpdateControlBackground(control);
            ThemeManager.UpdateControlForeground(control);

            if (useHistory)
            {
                if (_history.ContainsKey(control))
                {
                    _history[control] = Global.MainPanel.Children[0];
                }
                else
                {
                    _history.Add(control, Global.MainPanel.Children[0]);
                }
            }

            if (_timer == null)
            {
                _timer = new DispatcherTimer();
                _timer.Tick += TimerTick;
                _timer.Interval = new TimeSpan(Animation.TimerInterval * TimeSpan.TicksPerMillisecond);
            }
            if (_timer.IsEnabled) return;

            _animation = animation;

            Global.MainPanel.Children.Add(control);

            _animation.Initialize(control);

            _timer.Start();
        }

        public static void NavigateBack(this Control control)
        {
            NavigateBack(control, Animation.Fadeout);
        }

        public static void NavigateBack(this Control control, IAnimation animation)
        {
            if (_history.ContainsKey(control))
            {
                Navigate((Control)_history[control], animation, false);
            }
        }

        private static void TimerTick(object sender, EventArgs e)
        {
            if (Global.MainPanel.Children.Count > 1)
            {
                Control oldControl = (Control)Global.MainPanel.Children[0];
                Control newControl = (Control)Global.MainPanel.Children[1];

                if (!_animation.NextStep(oldControl, newControl))
                {
                    _timer.Stop();
                    Global.MainPanel.Children.Remove(oldControl);
                    oldControl.Opacity = 1;
                    oldControl.SetValue(Canvas.LeftProperty, 0.0);
                    oldControl.SetValue(Canvas.TopProperty, 0.0);
                }
            }
        }
    }
}
